<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button class="filter-item" style="margin-left: 5px;" type="primary" icon="el-icon-edit" @click="handleCreate">
        添加
      </el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      row-key="id"
      border
      default-expand-all
      fit
      highlight-current-row
      style="width: 100%;"
      max-height="600px"
    >
      <el-table-column label="权限名称" prop="name" align="center" />
      <el-table-column label="后端uri" prop="uri" align="center" />
      <el-table-column label="前端路由" prop="route" align="center" />
      <el-table-column label="是否菜单" align="center">
        <template slot-scope="{row}">
          <el-tag :type="row.menu ? 'success':'info'">
            {{ row.menu ? '是' : '否' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="图标" align="center">
        <template slot-scope="{row}">
          <i v-if="row.icon !== ''" :class="row.icon" />
        </template>
      </el-table-column>
      <el-table-column label="排序" prop="sort" align="center" />
      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button-group>
            <el-button type="primary" size="mini" @click="handleEdit(row)">
              编辑
            </el-button>
            <el-button size="mini" type="danger" @click="handleDelete(row)">
              删除
            </el-button>
          </el-button-group>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="temp"
        label-position="left"
        label-width="100px"
        style="width: 400px; margin-left:50px;"
      >
        <el-form-item label="父级权限" prop="parent_id">
          <el-tree-select v-model="temp.parentId" :render-after-expand="false" :tree-params="treeParams" />
        </el-form-item>
        <el-form-item label="权限名称" prop="name" required>
          <el-input v-model="temp.name" />
        </el-form-item>
        <el-form-item label="后端uri" prop="uri">
          <el-input v-model="temp.uri" />
        </el-form-item>
        <el-form-item label="前端路由" prop="router">
          <el-input v-model="temp.route" />
        </el-form-item>
        <el-form-item label="是否菜单" prop="menu">
          <el-switch v-model="temp.menu" />
        </el-form-item>
        <el-form-item label="图标" prop="icon">
          <el-input v-model="temp.icon" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="temp.sort" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          确认
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import request from '@/utils/request'

export default {
  name: 'List',
  data() {
    return {
      tableKey: 0,
      list: [],
      ruleList: [],
      treeParams: {
        clickParent: true,
        'default-expand-all': true,
        'expand-on-click-node': false,
        data: [],
        props: {
          children: 'children',
          label: 'name',
          disabled: 'disabled',
          value: 'id'
        }
      },
      listLoading: true,
      temp: {
        id: undefined,
        parentId: 0,
        name: '',
        uri: '',
        route: '',
        menu: true,
        icon: '',
        sort: 0
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑权限',
        create: '添加权限'
      },
      rules: {
        name: [{ required: true, message: '权限名称必填', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getList()
    this.getRuleList()
  },
  methods: {
    getList() {
      this.listLoading = true
      request({
        url: '/rule/list'
      }).then(response => {
        this.list = response.data
        this.listLoading = false
      })
    },
    getRuleList() {
      request({
        url: '/rule/selectList'
      }).then(response => {
        this.treeParams.data = response.data
      })
    },
    resetTemp() {
      this.temp = {
        id: undefined,
        parentId: 0,
        name: '',
        uri: '',
        route: '',
        menu: true,
        icon: '',
        sort: 0
      }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          request({
            url: '/rule/add',
            data: this.temp
          }).then(() => {
            this.dialogFormVisible = false
            this.$message({
              message: '添加成功',
              type: 'success',
              duration: 2000
            })
            this.getList()
          })
        } else {
          this.$message({
            message: '请完善必填项',
            type: 'error',
            duration: 2000
          })
        }
      })
    },
    handleEdit(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          request({
            url: '/rule/edit',
            data: this.temp
          }).then(() => {
            this.dialogFormVisible = false
            this.$message({
              message: '编辑成功',
              type: 'success',
              duration: 2000
            })
            this.getList()
          })
        } else {
          this.$message({
            message: '请完善必填项',
            type: 'error',
            duration: 2000
          })
        }
      })
    },
    handleDelete(row) {
      const id = row.id
      this.$msgbox.confirm(
        '确认删除？',
        'Warning',
        {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        request({
          url: '/rule/delete',
          data: { id: id }
        }).then(() => {
          this.$message({
            message: '删除成功',
            type: 'success',
            duration: 2000
          })
          this.getList()
        })
      })
    }
  }
}
</script>
